<template>
  <el-dialog :close-on-click-modal="false" modal-append-to-body lock-scroll top="10%"  v-if="email.isShow" @close="initAuthCode" class= "dislog-login dislog-phone" :title="$t('public.tip')" :visible.sync="email.isShow">
    <el-input size="mini" :placeholder="$t('login.emailCode')" v-model.trim="authCode" auto-complete="off"></el-input>
    <a class="email-resend__btn" @click.stop="retryMail2Authcode">{{$t('login.emailTip3')}}</a>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" :disabled="btn" @click.native.stop="signInByEmail">{{$t('login.loginBtn')}}</el-button>
    </div>
  </el-dialog>
</template>
<script>
  import NodeRSA from 'node-rsa'
  import { deepClone } from '@/utils/m7Utils.js'
  export default {
    name: 'Email',
    data () {
      return {
        authCode: '',
        btn: false
      }
    },
    props: {
      email: {
        type: Object,
        default: function () {
          return {
            isShow: true,
            lastCodeKey: '',
            form: null
          }
        }
      }
    },
    computed: {
      signName () {
        return this.btn ? this.$t('login.loginingBtn') : this.$t('login.loginBtn')
      }
    },
    methods: {
      signInByEmail () {
        this.btn = true
        let form = this.email.form
        form.lastCodeKey = this.email.lastCodeKey
        form.authCode = this.authCode
        this.authCode = ''
        if (!form.authCode) {
          this.btn = false
          this.$message.error(this.$t('login.emailCode'))
          return
        } else {
          this.$store.dispatch('getLoginSecretKey', {}).then(res1 => {
            if (res1.success) {
              let key = new NodeRSA(res1.secret)
              let _form = deepClone(form)
              let encrypted = key.encrypt(_form.password + ':' + res1.num, 'base64')
              _form.safeKey = encrypted
              // delete _form.password
              this.$store.dispatch('signIn', _form).then(val => {
                if (!val.success) {
                  this.btn = false
                }
                if (val.success) {
                  this.$emit('dialogSuccess')
                }
                if (this.$store.state.login) {
                  let ls = window.localStorage
                  let form = {}
                  if (this.email.form.remember) {
                    form = this.email.form
                  } else {
                    form = this.email.form
                    form.loginName = ''
                    form.password = ''
                  }
                  delete form.lastCodeKey
                  delete form.authCode
                  ls.setItem('loginForm', JSON.stringify(form))
                  this.$router.replace({path: '/index'})
                  this.email.isShow = false
                } else {
                  this.email.form.authCode = ''
                }
              })
            }
          })
        }
      },
      initAuthCode () {
        this.email.form.authCode = ''
        this.btn = false
      },
      retryMail2Authcode () {
        this.$store.dispatch('retryMail2Authcode', {lastCodeKey: this.email.lastCodeKey}).then(data => {
          this.btn = false
          this.$message({
            message: this.$t('login.emailTip2'),
            type: 'success'
          })
        })
      }
    }
  }
</script>
<style lang="stylus" scoped>
  .email-resend__btn
    display block
    margin-top 10px
</style>
